namespace MFWebSocket.Contract
{

    #region Interfaces

    /// <summary>
    /// An interface to which all WebSocketClient implementations must adhere
    /// </summary>
    public interface IWebSocketClient
    {
        /// <summary>
        /// Sends a message using the underlying WebSocketClient implementation
        /// </summary>
        /// <param name="message">The message to to be sent</param>
        void Send(string message);
        /// <summary>
        /// The event fired when the WebSocketClient has opened the connection
        /// </summary>
        event OpenHandler Open;
        /// <summary>
        /// The event fired when the WebSocketClient has closed the connection
        /// </summary>
        event CloseHandler Close;
        /// <summary>
        /// The event fired when the WebSocketclient has recieved a message
        /// </summary>
        event MessageHandler Message;
        
        /// <summary>
        /// Fires a Message event on the underlying WebSocketClient implementation
        /// </summary>
        /// <param name="sender">The WebSocketClientInstance who sent the message</param>
        /// <param name="args">The message sent on the underlying WebSocketClient implementation</param>
        void MessageHandler(IWebSocketClient sender, MessageArgs args);
    }

    #endregion

    #region MessageArgs

    /// <summary>
    /// A definition of the MessageArgs used in the Message event of a WebSocketClient
    /// </summary>
    public class MessageArgs : Microsoft.SPOT.EventArgs
    {

        /// <summary>
        /// Constructs a new MessageArgs
        /// </summary>
        /// <param name="message">The message of the MessageArgs</param>
        public MessageArgs(string message) { Message = message; }

        /// <summary>
        /// The string message of this MessageArgs
        /// </summary>
        protected string m_Message;

        /// <summary>
        /// The binary data of the MessageArgs
        /// </summary>
        public byte[] DataFrame { get; protected set; }

        /// <summary>
        /// The string message of this MessageArgs
        /// </summary>
        public string Message
        {
            get
            {
                return m_Message;
            }
            protected set
            {
                //Store the string representation of the Message
                m_Message = value;
                //Encode the DataFrame
                int frameIndex = value.Length;
                byte[] dataFrame = new byte[frameIndex + 2];
                System.Text.Encoding.UTF8.GetBytes(m_Message).CopyTo(dataFrame, 1);
                dataFrame[0] = MFWebSocket.Contract.Clients.WebSocketClient.m_DataFrameStart;
                dataFrame[frameIndex] = MFWebSocket.Contract.Clients.WebSocketClient.m_DataFrameEnd;
                DataFrame = dataFrame;
            }
        }

        /// <summary>
        /// Overrides Object.ToString
        /// </summary>
        /// <returns>The string message of this MessageArgs</returns>
        public override string ToString()
        {
            return m_Message;
        }
    }

    #endregion

    #region Delegates

    /// <summary>
    /// Signature used by the Message event and MessageHandler of a WebSocketClient implementation.
    /// </summary>
    /// <param name="sender">The WebSocketClient which sent the message</param>
    /// <param name="args">The message args</param>
    public delegate void MessageHandler(IWebSocketClient sender, MessageArgs args);

    /// <summary>
    /// Signate used by the Close event and the CloseHandler of a WebSocketClient implementation.
    /// </summary>
    /// <param name="sender">The WebSocketClient which closed it's connection</param>
    public delegate void CloseHandler(IWebSocketClient sender);

    /// <summary>
    /// Signature used by the Open event and the OpenHandler of a WebSocketClient implementation.
    /// </summary>
    /// <param name="sender">The WebSocketClient which has connected to it's host</param>
    public delegate void OpenHandler(IWebSocketClient sender);

    #endregion

}